<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>应用管理</title> {{>/WEB-INF/pages/common/meta_base.part.html}}
{{>/WEB-INF/pages/common/refs_base.part.html}}
{{>/WEB-INF/pages/common/refs_modal.part.html}}
{{>/WEB-INF/pages/common/refs_plupload.part.html}}
</head>
<body>
	<!-- <div class="container"> -->
	{{>/WEB-INF/pages/common/layout_header.part.html}}

	<div class="container">
		<ul class="breadcrumb">
			<li><a href="{{ctx}}">首页</a></li>
			<li><a href="{{ctx}}/mam/appMain/list-view">应用管理</a></li>
			<li><a href="{{ctx}}/mam/appMain/app-view/{{appvo.id}}">{{appvo.app_name}}</a></li>
			<li class="active">{{appVersion.version}}版本({{appVersion.platformid}})</li>
		</ul>
		<hr class="hr1" />
		<div class="hr-10"></div>
		<div class="container-fluid bor-tab bg-st-white pad-st-a10">
			<div id="tabs-604528" class="tabbable">
				<ul class="nav nav-tabs">
					<li class="active"><a data-toggle="tab" href="#panel-1"
						id="panel-1_active">基本信息</a></li>
					<li class=""><a data-toggle="tab" href="#"
						data-target="#panel-2" id="panel-2_active">关联模块</a></li>
					<li class=""><a data-toggle="tab" href="#"
						data-target="#panel-3" id="panel-3_active">生成应用</a></li>
				</ul>
				<div class="tab-content">
					<div id="panel-1" class="tab-pane active">
						<form id="dataForm" name="dataForm" method="post"
							enctype="multipart/form-data">
							<input type="hidden" name="applicationid" value="{{appvo.id}}"
								id="applicationid" /> <input type="hidden" name="version"
								value="{{appVersion.version}}" id="appVersion" /> <input
								type="hidden" name="build" value="{{appVersion.build}}"
								id="buildInput" /> <input type="hidden" name="platformid"
								value="{{appVersion.platformid}}" id="platformid" /> <input
								type="hidden" name="release_note"
								value="{{appVersion.release_note}}" id="release_note" /> <input
								type="hidden" name="status" value="{{appVersion.status}}"
								id="status" />
							<div class="mar-st-a30">
								<div class="row pad-st-b10">
									<div class="text-right pad-st-b5">
										<a href="#" onclick="enterEdit()"
											class="btn btn-primary btn-sm tls" data-toggle="modal">编辑</a>
										<a href="#" onclick="deleteAppVersion()"
											class="btn btn-common btn-sm tls" data-toggle="modal">删除</a>
									</div>
									<div class="col-sm-2 text-center">
										<img class="img-rounded" id="appIcon"
											style="width: 106px; height: 106px;" />
										<p class="text-muted">版本号：{{appVersion.version}}</p>
									</div>
									<div class="col-sm-9 text-left">

										<ul class="list-li">
											<li>应用名称：{{appvo.app_name}}</li>
											<li>应用标识：{{appvo.identifier}}</li>
											<li>应用类别：{{appvo.appCategoryVo.name}}</li>
											<li>构建号：{{appVersion.build}}</li>
											<li>下载量：{{appVersion.version_counter}}</li>
											<li>应用版本：{{appVersion.version}}</li>
											<li>创建时间：<script type="text/javascript">
												document
														.write($.format
																.date(
																		"{{appVersion.create_time}}",
																		"yyyy-MM-dd HH:mm"))
											</script>
											</li>
											<li>更新时间：<script type="text/javascript">
												document
														.write($.format
																.date(
																		"{{appVersion.modified_time}}",
																		"yyyy-MM-dd HH:mm"))
											</script></li>
											<li>版本描述：<span>{{appVersion.release_note}}</span></li>
											<li id="platDownDIV">安装包下载： <a class="btn-info btn-sm"
												style="line-height: 28px; display: none"
												href="{{ctx}}/mam/apps/download/{{appvo.identifier}}/{{appVersion.version}}/{{appVersion.platformid}}?appKey={{appKey}}"
												id="{{id}}">{{appVersion.platformid}}安装包</a> <script>
													var bunble = "{{appVersion.bundle}}";
													var id = "{{id}}"
													if (bunble != null
															&& bunble != "") {
														$("#" + id).show();
													}
												</script>
											</li>
											<li id="plistDownDIV" style="display: none">plist： <a
												class="btn-info btn-sm" style="line-height: 28px;"
												href="{{ctx}}/mam/attachment/download/{{plist}}">plist下载</a>
											</li>
											<li>版本状态：<span class="label label-info" id="lisapn"></span></li>
										</ul>
									</div>
								</div>
								<div class="hr mar-st-tb10"></div>
								<div class="row" id="rowId">

									<div class="col-sm-4 text-left" id="divPanel" style="display:">
										<div class="box-st-bg">
											<img
												src="{{ctx}}/static/bsl/images/{{appVersion.platformid}}002.gif"
												style="height: 90px" class="img-rounded" />
											<div class="btn-info img-rounded" id="statusinfo">未激活</div>
											<br />
											<div>
												<a
													onclick="showMyCert('{{appVersion.id}}','{{appVersion.platformid}}')"
													class="btn btn-primary btn-sm">上传安装包</a> <a
													class="btn btn-primary btn-sm"
													onclick="enterPackCert('{{appVersion.applicationid}}','{{appVersion.version}}','{{appVersion.platformid}}')">生成安装包</a>
											</div>
											<input type="hidden" name="task_id"
												value="{{appVersion.task_id}}" id="packageSeq" />
										</div>
									</div>
								</div>
								<div class="container pad-st-b30 text-right">
									<button class="btn btn-primary btn-sm tls" type="button"
										id="publishBtn">上架</button>
									<a type="button" class="btn btn-common btn-sm tls"
										href="{{ctx}}/mam/appMain/app-view/{{appvo.id}}">返回</a>
								</div>
							</div>
						</form>
						<div class="bor-top">
							<div class="tit2-st he30px">
								<span>应用快照 </span>
							</div>
							<div class="text-right pad-st-b5">
								<a class="btn btn-primary btn-sm tls" data-toggle="modal"
									href="#appSnapshotModal">新增快照</a>
							</div>
							<div class="bor-top">
								{{#appVersion.snapshotIds}}
								<div class="col-sm-2 text-center pad-st-b10" id="noIcon">
									<ul class="ul_four">
										<li><img class="info-icon"
											src="{{ctx}}/mam/attachment/download/{{.}}"
											style="height: 106px; width: 106px;" /></li>
										<li><img class="img-circle delete-icon"
											data-toggle="tooltip" data-placement="right" title="删除"
											style="cursor: pointer"
											src="{{ctx}}/static/bsl/images/XX.png"
											onclick="deleteSnaphot('{{appVersion.id}}','{{.}}')" /></li>
									</ul>

								</div>
								{{/appVersion.snapshotIds}}

								<div class="hr"></div>
							</div>
						</div>
					</div>
					<div id="panel-2" class="tab-pane">
						<div class="container-fluid text-center">
							<img src="{{ctx}}/static/bsl/images/loadImage.gif" border="0"
								style="width: 50px; height: 50px" alt="loading..." id="load" />
						</div>
					</div>
					<div id="panel-3" class="tab-pane"></div>
					<!--  panel-3结束-->
				</div>
			</div>
		</div>
	</div>
	{{>/WEB-INF/pages/app/widget-modal.html}}
	<!-- 选择证书 -->
	{{>/WEB-INF/pages/app/pack-modal.html}}
	<!-- 上传快照 -->
	{{>/WEB-INF/pages/app/snapshot-modal.html}}
	<!-- footer 脚本 -->
	{{>/WEB-INF/pages/common/layout_footer.part.html}}
</body>
</html>

<script type="text/javascript">
	$(function() {
		var icon_id = "{{appvo.icon}}";
		if (icon_id != "") {
			jQuery("#appIcon").attr("src",
					"{{ctx}}/mam/attachment/download/{{appvo.icon}}");
		} else {
			jQuery("#appIcon").attr("src",
					"{{ctx}}/static/bsl/images/defind.png");
		}

		$('#panel-2_active').click(function(e) {
			e.preventDefault();
			var appId = '{{appVersion.applicationid}}';
			var version = '{{appVersion.version}}';
			var platform = '{{appVersion.platformid}}';
			reloadPanel(platform, version, appId)
			$(this).tab('show')
		});

		$('#panel-3_active').click(
				function(e) {
					e.preventDefault();
					$('#panel-3').empty();
					var appId = '{{appVersion.applicationid}}';
					var version = '{{appVersion.version}}';
					var platform = '{{appVersion.platformid}}';
					var loadurl = "{{ctx}}/mam/logos/" + platform + "/"
							+ version + "/" + appId;
					$.get(loadurl, function(data) {
						$('#panel-3').html(data)
					});
					$(this).tab('show')
				});
		reloadBox();
	});

	//文档加载时，检测应用的版本的状态
	function reloadBox() {
		var ispub = false;
		var status = '{{appVersion.status}}';
		var parent = $('#statusinfo').parent();
		if (status === "0") {
			$('#statusinfo').text("打包失败");
			$('#lisapn').text("打包失败");
			var atag = parent.find('a');
			$(atag).each(function(index) {
				$(this).removeAttr("disabled");
			});
		} else if (status === "2") {
			var loadImage = $("<img>").attr("src",
					"{{ctx}}/static/bsl/images/loadImage.gif").attr("style",
					"width: 20px; height: 20px;");
			$('#statusinfo').empty();
			$('#statusinfo').append("正在打包...");
			$('#statusinfo').append(loadImage);
			$('#lisapn').text("打包中");
			var atag = parent.find('a');
			$(atag).each(function(index) {
				$(this).attr("disabled", "disabled");
			});
			reloadInterval();
		} else if (status === "3") {
			$('#statusinfo').text("已激活");
			$('#lisapn').text("已激活");
			var atag = parent.find('a');
			$(atag).each(function(index) {
				$(this).removeAttr("disabled");
			});
		} else if (status === "4") {
			$('#statusinfo').text("已上架");
			$('#lisapn').text("已上架");
			ispub = true;
		} else if (status === "5") {
			$('#statusinfo').text("已下架");
			$('#lisapn').text("已下架");
		} else {
			$('#statusinfo').text("新建");
			$('#lisapn').text("新建");
		}

		//如果是上架状态，则不允许操作，否则可以进行上传包操作
		if (ispub) {
			$('#rowId').find('a').each(function(index) {
				$(this).removeAttr("onclick");
				$(this).attr("disabled", "disabled");
			});
			$('#publishBtn').text("下架");
			$('#publishBtn').unbind();
			$('#publishBtn').bind("click", function() {
				downApp();
			});
		} else {
			$('#publishBtn').unbind();
			$('#publishBtn').bind("click", function() {
				publishApp();
			});
		}
	}

	//定时器刷新打包状态
	var interval;
	function reloadInterval() {
		interval = window.setInterval(function() {
			findByInterval()
		}, 10000);
	}
	function findByInterval() {
		var status = $("#status").val();
		var packageSeq = $('#packageSeq').val();
		var parent = $('#packageSeq').parent();
		if (status === "2") {//是打包状态，则进行定时器执行
			var url = "{{ctx}}/mam/pkg/findBySeq" + "?task_id=" + packageSeq
					+ "&t=" + new Date();
			$.ajax({
				async : false,
				type : "GET",
				url : url,
				dataType : 'json',
				success : function(data, textStatus) {
					if (data.result === "success") {
						$('#status').val(data.status);
						if (data.status != "2") {//打包结束
							var atag = parent.find('a');
							$(atag).each(function(index) {
								$(this).removeAttr("disabled");
							});
							if (data.status === "3") {
								$("#statusinfo").text("已激活");
								$("#lisapn").text("已激活");
								$('#platDownDIV').find("a").show();
							} else {//打包失败，隐藏下载按钮
								$("#statusinfo").text("打包失败");
								$("#lisapn").text("打包失败");
								$('#platDownDIV').find("a").hide();
							}
							uploadBundle(data.bundle);
							clearInterval(interval);//移除定时器
						} else {//打包状态，隐藏下载按钮
							$('#platDownDIV').find("a").hide();
						}
					}
				}
			});
		}

	}

	//重新加载模块
	function reloadPanel(platform, version, appId) {
		var loadurl = "{{ctx}}/mam/appwidgets/" + platform + "/" + version
				+ "/relations/" + appId + "?t=" + new Date();
		$.get(loadurl, function(data) {
			$('#panel-2').html(data)
		});
	}

	//提示框
	var popover = function(inputId, content) {
		var options = {
			placement : "bottom",
			content : content
		};
		$('#' + inputId).attr("data-toggle", "popover");
		$('#' + inputId).popover(options);
		$('#' + inputId).popover('show');
	}

	//打开上传安装包对话框
	function showMyCert(versionId, platformid) {
		$('#platformid').val(platformid);
		//设置上传安装包的平台
		$('#uploadPack').find("#platform").val(platformid);
		$('#myPackbtn').unbind();
		$('#myPackbtn').bind("click", function() {
			updateBundle(versionId);
		});
		$('#uploadPack').modal("show");
	}

	function uploadBundle(bundle) {
		var versionId = "{{appVersion.id}}";
		$.ajax({
			async : true,
			type : "POST",
			url : "{{ctx}}/mam/pkg/bundle/" + versionId + "?bundle=" + bundle,
			dataType : 'json',
			success : function(data, textStatus) {
			},
			error : function(data, status, e) {
				bootbox.alert("系统繁忙，请稍后再试！");
			}
		});
	}

	function enterPackCert(appId, version, platform) {
		$('#panel-3_active').click();
	}

	function publishApp() {
		//获取激活状态
		var status = $('#status').val();
		if (status === "0" || status === "1" || status === "2") {
			//bootbox.alert("该版本还没有激活，请先上传安装包或者生成安装包!");
			bootbox.alert("该版本还没有激活，请先上传安装包!");
			return;
		}
		var pubVersion = "{{pubVersion}}";
		var msgTip = "您确定要替换架上版本吗?";
		if (pubVersion == "") {
			msgTip = "您确定要上架此版本吗?";
		}
		bootbox
				.confirm(
						msgTip,
						function(result) {
							if (result) {
								var url = "{{ctx}}/mam/appversions/online/{{appVersion.platformid}}/{{appVersion.id}}";
								$.ajax({
									async : true,
									type : "PUT",
									url : url,
									dataType : 'json',
									success : function(data, textStatus) {
										if (data.result === "success") {
											bootbox.alert("上架成功！", function() {
												//window.location.href = "{{ctx}}/mam/appMain/app-view/{{appvo.id}}";
												window.location.reload();
											});
										} else {
											bootbox.alert("上架失败，请稍后再试！");
										}
									},
									error : function(data, status, e) {
										bootbox.alert("系统繁忙，请稍后再试！");
									}
								});
							}
						});
	}

	//下架
	function downApp() {
		var url = "{{ctx}}/mam/appversions/offline/{{appVersion.platformid}}/{{appVersion.id}}";
		$.ajax({
			async : true,
			type : "PUT",
			url : url,
			dataType : 'json',
			success : function(data, textStatus) {
				if (data.result === "success") {
					bootbox.alert("下架成功！", function() {
						window.location.reload();
					});
				} else {
					bootbox.alert("下架失败，请稍后再试！");
				}
			},
			error : function(data, status, e) {
				bootbox.alert("系统繁忙，请稍后再试！");
			}
		});

	}

	function enterEdit() {
		var op = {
			remote : "{{ctx}}/mam/appversions/{{appVersion.platformid}}/{{appVersion.version}}/{{appVersion.id}}",
			show : true
		};
		$('#btn_modal').unbind();
		$('#btn_modal').bind("click", function() {
			formSubmit();
		});
		$('#myModal').modal(op);
	}

	//删除应用版本
	function deleteAppVersion() {
		var status = "{{appVersion.status}}";
		if (status == "4") {
			bootbox.alert("该版本已上架，无法进行删除!");
			return;
		}

		var url = "{{ctx}}/mam/appversions/{{appVersion.platformid}}/{{appVersion.version}}/{{appVersion.id}}";
		bootbox
				.confirm(
						"该操作将会删除该版本,是否确认删除?",
						function(op) {
							if (op) {
								$
										.ajax({
											type : "DELETE",
											dataType : "json",
											url : url,
											success : function(data, textStatus) {
												if (data.hasPub == "true") {
													bootbox
															.alert("该版本已上架，无法删除");
												} else if (data.result == "success") {
													var appurl = "{{ctx}}/mam/appMain/app-view/{{appvo.id}}";
													bootbox
															.alert(
																	"删除成功",
																	function() {
																		window.location.href = appurl;
																	});
												} else {
													bootbox
															.alert("删除失败，请稍后再试！");
												}
											},
											error : function(data, status, e) {
												bootbox.alert("系统繁忙，请稍后再试！");
											}
										});
							}
						});
	}

	//删除应用快照
	function deleteSnaphot(appVId, snapshotId) {
		var url = "{{ctx}}/mam/appversions/{{appVersion.platformid}}/snapshot/"
				+ snapshotId + "?appVId=" + appVId;
		bootbox.confirm("确定要删除吗?", function(result) {
			if (result) {
				$.ajax({
					type : "DELETE",
					dataType : "json",
					url : url,
					success : function(data, textStatus) {
						if (data.result == "success") {
							bootbox.alert("删除成功！", function() {
								window.location.reload();
							});
						} else {
							bootbox.alert("删除失败，请稍后再试！");
						}
					},
					error : function(data, status, e) {
						bootbox.alert("系统繁忙，请稍后再试！");
					}
				});
			}
		});
	}
</script>